<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['banner','notice_list','new_notice','city_hehuoren'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 轮播图
     */
    public function banner(){
        $list = db('banner')
            ->field('id,image')
            ->order('id DESC')//倒叙
            ->select();
        $this->success('ok',$list);
    }

    /**
     * 系统通知列表
     */
    public function notice_list(){
        $page = input('page',1);
        $limit = input('limit',10);
        $list = db('notice')
            ->order('id DESC')
            ->page($page,$limit)
            ->select();
        foreach ($list as $key => $value){
            $list[$key]['createtime'] = date('Y-m-d',$value['createtime']);
        }
        $this->success('ok',$list);
    }
    /**
     * 首页最新通知
     */
    public function new_notice(){
        $data = db('notice')
            ->order('id DESC')
            ->find();
//        $data['content'] = strip_tags($data['content']);
        if (mb_strlen($data['content']) > 50){
            $data['content'] = mb_substr($data['content'],0,50).'...';
        }
        $this->success('ok',$data);
    }

    /**
     * 首页CB CC信息
     */
    public function money_info(){
        $user = $this->auth->getUser();
//        if ($user->is_truename == 1){
//            $this->error('请先通过实名验证');
//        }
        $cc = db('cc_price')->order('id DESC')->find();
        $cb = db('cb_price')->order('id DESC')->find();
        $cc_price  = $cc['price']??0;
        $cb_price = $cb['price']??0;
        $cny = db('config')->where('name','to_cny')->value('value');//cny转化比
        $data = [];
        $data['cb'] = [];
        $data['cc'] = [];
        $data['cb']['price'] = $cb_price;//价格
        $data['cb']['ke_money'] = $user->cb_num;//可用
        $data['cb']['count_money'] = round($user->cb_freeze + $user->cb_num,2);//总数量 = 冻结 + 可用
        $data['cb']['cny'] = round($cb_price * $cny/100,2);

        $data['cc']['price'] = $cc_price;//价格
        $data['cc']['ke_money'] = $user->cc_num;//可用
        $data['cc']['count_money'] = round($user->cc_freeze + $user->cc_num,2);//总数量 = 冻结 + 可用
        $data['cc']['cny'] = round($cc_price * $cny/100,2);


        $usercount_jishu = Config::get('site.usercount') ??0;//cb求购增加基数
        $today_jishu = Config::get('site.todayadd')??0;//cb卖出增加基数

        $data['user']['count'] = db('user')->count() +$usercount_jishu;
        $data['user']['new'] = db('user')->whereTime('createtime','d')->count() +$today_jishu;
        $this->success('ok',$data);
    }

    /**
     * 城市合伙人
     */
    public function city_hehuoren(){
        $data = db('config')->where('name','hehuoren')->value('value');
        $url = $this->request->domain();
        $data = str_replace('src="','style="width:100%" src="'.$url,$data);
        $this->success('ok',$data);
    }
}
